package com.zhrj.exam.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zhrj.exam.common.Result;
import com.zhrj.exam.entity.BladeUser;
import com.zhrj.exam.entity.TokenData;
import com.zhrj.exam.service.BladeUserService;
import com.zhrj.exam.service.GrantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/auth")
public class LoginController {
    @Resource
    private GrantService grantService;
    @Resource
    private BladeUserService bladeUserService;

    private static final String AUTHURL = "http://localhost:80/blade-auth/token";

    private static final String URL = "http://localhost:80/blade-user/user-list?pageNum=1&pageSize=10";

    //调用三方接口认证登录
    @GetMapping("/token")
    public Result<TokenData> login() throws JsonProcessingException {
        log.info("登录认证...");
        TokenData grant = grantService.grant(AUTHURL);
        return Result.success(grant);
    }

    @GetMapping("/list")
    //请求三方的用户信息台账查询
    public Result<List<BladeUser>> getUserList() throws JsonProcessingException, InterruptedException {
        log.info("请求三方用户信息台账查询...");
        List<BladeUser> bladeUsers = bladeUserService.pageQuery(URL);
        return Result.success(bladeUsers);
    }

}
